package atmSystem;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args) {

        // 准备系统存储账户的容器
        ArrayList<Account> accounts = new ArrayList<>();

        while (true){
            System.out.println("========欢迎您进入黑马银行ATM系统========");
            System.out.println("1.登录账户");
            System.out.println("2.注册开户");

            Scanner sc = new Scanner(System.in);
            System.out.print("请输入命令1、2选择对应的操作：");

            int command = sc.nextInt();
            switch (command){
                case 1:
                    login(accounts);
                    break;
                case 2:
                    register(accounts);
                    break;
                default:

            }
        }

    }

    /**
      登录功能
     */
    public static void login(ArrayList<Account> accounts) {
        System.out.println("========黑马银行ATM系统登录界面========");
        if(accounts.size() > 0){
            while (true) {
                Scanner sc = new Scanner(System.in);
                System.out.print("请您输入您的卡号：");
                String cardId = sc.next();


                // 先判断是否存在卡号
                Account account = getAccountByCardId(accounts , cardId);
                if(account == null){
                    // 说明卡号错误了
                    System.out.println("不存在该卡号");
                }else{
                    while (true) {
                        // 说明卡号存在
                        System.out.print("请您输入您的密码：");
                        String passWord = sc.next();
                        if(account.getPassWord().equals(passWord)){
                            // 登录成功了
                            System.out.println(account.getUserName()+"贵宾，欢迎您进入系统，您的卡号：" + account.getCardId());
                            // 展示其他操作功能
                            showUserCommand(accounts,account);
                            return;
                        }else {
                            // 密码错误了
                            System.out.println("您的密码有误，请确认！");
                        }
                    }
                }
            }


        }else {
            System.out.println("当前系统无任何账户，请注册账户");
        }
    }

    private static void showUserCommand(ArrayList<Account> accounts, Account account) {
        System.out.println("========欢迎您进入黑马银行用户操作界面========");
        while (true) {
            System.out.println("1、查询：");
            System.out.println("2、存款：");
            System.out.println("3、取款：");
            System.out.println("4、转账：");
            System.out.println("5、修改密码：");
            System.out.println("6、退出：");
            System.out.println("7、注销当前账户：");
            System.out.print("您可继续选择功能进行操作了：");
            Scanner sc = new Scanner(System.in);
            int command = sc.nextInt();
            switch (command){
                case 1:
                    showAccount(account);
                    break;
                case 2:
                    depositMoney(account);
                    break;
                case 3:
                    drawMoney(account);
                    break;
                case 4:
                    transferMoney(accounts , account);
                    break;
                case 5:
                    updatePassWord(account);
                    return;
                case 6:
                    // 退出系统功能
                    return;
                case 7:
                    // 注销当前账户
                    accounts.remove(account);
                    break;
                default:
                    System.out.println("您输入的命令不对，请重新确认！");
            }
        }
    }

    private static void updatePassWord(Account account) {
        System.out.println("========欢迎您进入黑马银行密码修改界面========");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请您输入当前账户的密码：");
            String passWord = sc.next();
            if(account.getPassWord().equals(passWord)){
                while (true){
                    System.out.println("请您输入新的密码：");
                    String newPassWord = sc.next();
                    System.out.println("请您确认下新密码：");
                    String okPassWord = sc.next();
                    if(newPassWord .equals(okPassWord) ){
                        account.setPassWord(okPassWord);
                        System.out.println("密码修改成功,请您重新登录！");
                        return;
                    }else{
                        System.out.println("两次密码不一致");
                    }
                }
            }else {
                System.out.println("当前账户密码不正确！");
            }
        }
    }

    private static void transferMoney(ArrayList<Account> accounts, Account account) {
        System.out.println("========欢迎您进入黑马银行用户转账界面========");
        if(accounts.size() >= 2 ){
            if(account.getMoney() > 0){
                Scanner sc = new Scanner(System.in);
                while (true) {
                    System.out.println("请您输入转账的账户卡号：");
                    String cardId = sc.next();
                    Account account1 = getAccountByCardId(accounts ,cardId);
                    if(account1 == null){
                        System.out.println("不存在该账户，请重新确认！");
                    }else {
                        String name = "*" + account1.getUserName().substring(1);
                        System.out.println("您当前要为"+ name +"转账！");
                        System.out.print("请您输入姓氏确认：");
                        String preName = sc.next();
                        if(account1.getUserName().startsWith(preName)){
                            System.out.println("请您输入转账的金额：");
                            double money = sc.nextDouble();
                            if(money > account.getMoney()){
                                System.out.println("您的账户余额不足");
                            }else {
                                account.setMoney(account.getMoney() - money);
                                account1.setMoney(account1.getMoney() + money);
                                System.out.println("您已经转账成功！：");
                                showAccount(account);
                                break;
                            }
                        }else {
                            System.out.println("您输入的姓氏不正确");
                        }
                    }
                }
            }else {
                System.out.println("您自己都没钱，就别转了！");
            }
        }else {
            System.out.println("当前系统，账户不足2个，不能转账！");
        }

    }

    private static void drawMoney(Account account) {
        System.out.println("========欢迎您进入黑马银行用户取款界面========");
        if(account.getMoney() >= 100){
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("请您输入取款的金额");
                double money = sc.nextDouble();
                if(money > account.getQuotaMoney()){
                    System.out.println("您当前取款超过了当日限额！");
                }else{
                    if(money > account.getMoney()){
                        System.out.println("您的账户余额不足");
                    }else {
                        account.setMoney(account.getMoney() - money);
                        System.out.println("您已经取款成功！：");
                        break;
                    }
                }
            }
            showAccount(account);
        }else{
            System.out.println("账户余额不足100元，先去存钱吧！");
        }


    }

    private static void depositMoney(Account account) {
        System.out.println("========欢迎您进入黑马银行用户存款界面========");
        System.out.println("请您输入存款的金额");
        Scanner sc = new Scanner(System.in);
        double money = sc.nextDouble();
        account.setMoney(money + account.getMoney());
        System.out.println("您已经存款成功！：");
        showAccount(account);
    }

    public static void showAccount(Account account) {
        System.out.println("========欢迎您进入黑马银行用户详情界面========");
        System.out.println("您的账户信息如下：");
        System.out.println("卡号：" + account.getCardId());
        System.out.println("姓名：" + account.getUserName());
        System.out.println("余额：" + account.getMoney());
        System.out.println("当日取现额度：" + account.getQuotaMoney());
    }

    public static Account getAccountByCardId(ArrayList<Account> accounts, String cardId){
        for (int i = 0; i < accounts.size(); i++) {
            Account account = accounts.get(i);
            if(account.getCardId().equals(cardId)){
                return account;
            }
        }
        return null;
    }


    /**
       注册功能
     */
    public static void register(ArrayList<Account> accounts) {
        System.out.println("========欢迎您进入黑马银行用户办卡界面========");
        Account account = new Account();

        Scanner sc = new Scanner(System.in);
        System.out.println("请您输入您的姓名：");
        String name = sc.next();
        account.setUserName(name);

        while (true){
            System.out.println("请您输入您的密码：");
            String passWord = sc.next();
            System.out.println("请您再次确认密码：");
            String okPassWord = sc.next();
            if(passWord.equals(okPassWord)){
                account.setPassWord(passWord);
                break;
            }else {
                System.out.println("您的确认密码不对，请重新输入");
            }
        }

        System.out.println("请设置当日取现额度：");
        double quotaMoney = sc.nextDouble();
        account.setQuotaMoney(quotaMoney);


        // 注意：必须为用户开户自动生成一个卡号：生成8位随机的数字作为卡号，但是卡号不可以重复的。
        while (true){
            String cardId = createCardId();
            if(!existCardId(accounts , cardId)){
                // 不存在该卡号
                account.setCardId( cardId);
                break;
            }
        }

        accounts.add(account);
        System.out.println(name+"贵宾，您的账户已经开卡成功，您的卡号是：" + account.getCardId());

    }

    public static boolean existCardId(ArrayList<Account> accounts , String cardId){
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if(acc.getCardId().equals(cardId)){
                return true;
            }
        }
        return false;
    }

    public static String createCardId(){
        String cardId = "";
        Random r = new Random();
        for (int i = 0; i < 8; i++) {
            int data = r.nextInt(10); // 0-9
            cardId += data;
        }
        return cardId;
    }
}
